package com.lin.reggie.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lin.reggie.entity.*;
import com.lin.reggie.mapper.*;
import com.lin.reggie.service.OrderService;
import com.lin.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;//地址
    @Autowired(required = false)
    private UserMapper userMapper;//用户
    @Autowired(required = false)
    private OrderMapper orderMapper;//订单
    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;//订单明细
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;//购物车


    public Page<OrderDetail>find(Integer page,Integer pageSize,Long userId){
        PageHelper.startPage(page,pageSize);
        Orders orders = orderMapper.findByUserId(userId);
        Long ordersId = orders.getId();
        List<OrderDetail> orderDetailList = orderDetailMapper.findByOrderId(ordersId);
        PageInfo<OrderDetail> pageInfo = new PageInfo<>(orderDetailList);
        Page<OrderDetail> page1 = new Page<OrderDetail>(pageInfo.getList(), pageInfo.getTotal(), pageSize, page);
        return  page1;

    }

    @Override
    @Transactional
    public void addOrder(Orders orders, Long userId) {
        //获取地址id
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());
        //根据传递过来的登录用户id获取购物车数据list<shoppingcart>
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findCartByUserId(userId);
        //根据用户的id获取登录用户对象
        User user = userMapper.findById(userId);
        //完善订单对象order数据
        Long oid = new Long(UUIDUtils.getUUIDInOrderId());
        //Order订单表设置id传入newlong的对象
        orders.setId(oid);
        orders.setNumber(oid+"");
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setUserName(user.getName());
                  //联系人/收件人
        orders.setConsignee(addressBook.getConsignee());
        //完善订单明细对象list<orderdetail>数据
        List<OrderDetail> orderDetailList = new ArrayList<>();
        //定义总金额
        BigDecimal total = new BigDecimal("0");
        //遍历购物车集合数据，封装每个orderDetail的对象
        for (ShoppingCart shoppingCart : shoppingCartList) {
            OrderDetail orderDetail = new OrderDetail();
            Long orderDetailId = new Long(UUIDUtils.getUUIDInOrderId());
            orderDetail.setId(orderDetailId);
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());
            //封装好的orderDeail对象加入到orderDtailList集合里
            orderDetailList.add(orderDetail);
            //累加每个商品的金额= 数量×单价
            //multiply（）这个decimal类型的乘法方法
            //add（）这个是decimal类型的加法方法
            //总金额=                      金额      ×            数量
            total=total.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber()+"")));
            //完善总金额
            orders.setAmount(total);}
            //调用订单主表接口方法 传入数据
            orderMapper.save(orders);
            //调用订单明细表存入数据
            orderDetailMapper.saveBatch(orderDetailList);
            //下单成功后，清空购物车
            shoppingCartMapper.deleteShoppingCart(userId);

    }
}
